/**
 * 离线操作
 * Created by WMM on 2018/06/18.
 */
import { Component } from '@angular/core';
import { IonicPage, NavController, ToastController, NavParams, AlertController, LoadingController, App } from 'ionic-angular';
import { OfflineDataApi } from '../../api/offline-data/OfflineDataApi';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite'

@IonicPage()
@Component({
  selector: 'offline-operation',
  templateUrl: 'offline-operation.html'
})
export class OfflineOperation {
  myAppDatabase: SQLiteObject;




  //  页面即将进入时调用
  ionViewWillEnter() {
    this.getData();
  }

  constructor(private sqlite: SQLite, public offlineDataApi: OfflineDataApi, private app: App, public navCtrl: NavController, private loadingCtrl: LoadingController, private alertCtrl: AlertController, public params: NavParams, private toastCtrl: ToastController) {
  }

  //将所有的表都初始化以防后期查询出错
  getData() {
    this.sqlite.create({
      name: 'ionicdb.db',
      location: 'default'
    }).then((db: SQLiteObject) => {
      db.executeSql('CREATE TABLE IF NOT EXISTS materiel(rowid INTEGER PRIMARY KEY ,date TEXT,companyId VARCHAR,name TEXT,code TEXT,qrcode TEXT,length decimal(50,2), width decimal(50,2), height decimal(50,2),emptyWeight decimal(50,3),volume decimal(50,3),takePhoto INT,materielId VARCHAR,pklmaterielId VARCHAR)', [])
        .then(res => console.log('初始化表 materiel'))
        .catch(e => console.log(e.messagee));
      db.executeSql('CREATE TABLE IF NOT EXISTS container(rowid INTEGER PRIMARY KEY ,date TEXT,companyId VARCHAR,id VARCHAR,name TEXT,code TEXT, qrcode TEXT,length decimal(50,3), width decimal(50,2), height decimal(50,2),deadWeight decimal(50,3),bearWeight decimal(50,3),containerType INT)', [])
        .then(res => console.log('初始化表 container'))
        .catch(e => console.log(e.message));
      db.executeSql('CREATE TABLE IF NOT EXISTS loadOrUnload(rowid INTEGER PRIMARY KEY ,date TEXT,containerId VARCHAR ,containerName VARCHAR,containerCode VARCHAR, containerQrcode TEXT, materielId VARCHAR, materielName VARCHAR, materielCode VARCHAR, materielQrcode TEXT, pklmaterielId VARCHAR, bizType INT,img VARCHAR,imgType VARCHAR)', [])
        .then(res => console.log('初始化表 loadOrUnload'))
        .catch(e => console.log(e.message));
      db.executeSql('CREATE TABLE IF NOT EXISTS ruleRecord(rowid INTEGER PRIMARY KEY ,date TEXT, materielQrcode TEXT, length decimal(50,3), width decimal(50,3), height decimal(50,3),weight decimal(50,3),description TEXT,img VARCHAR,materielId VARCHAR,materielName VARCHAR, materielCode VARCHAR,pklmaterielId VARCHAR)', [])
        .then(res => console.log('初始化表 ruleRecord'))
        .catch(e => console.log(e.messagee));
      db.executeSql('CREATE TABLE IF NOT EXISTS exceptionReport(rowid INTEGER PRIMARY KEY ,date TEXT,exception TEXT)', [])
        .then(res => console.log('初始化表 exceptionReport'))
        .catch(e => console.log(e.messagee));
      this.myAppDatabase = db;
    }).catch(e =>
      this.showMsg("系统Sql组件错误,请联系管理员!,错误信息为: " + e.messagee)
    );
  }

  logIn() {
    let alert = this.alertCtrl.create({
      title: '使用在线功能请确保网络通畅!',
      message: "点击确定将进入登录页面!",
      buttons: ['取消',
        {
          text: '确定',
          handler: data => {
            //点击确定,跳转到登录页面
            this.app.getRootNav().push('LoginPage', { "backButtontag": true });//用this.app.getRootNav() 从根页面跳转
          }
        }]
    });
    alert.present();
  }


  //点击取消返回
  goBack() {
    this.navCtrl.pop();
  }

  showMsg(msg: string) {
    var toast = this.toastCtrl.create({
      message: msg,
      duration: 3000,
      position: 'bottom'
    });
    toast.present();
  }


  goRefreshCache() {
    this.navCtrl.push('OfflineCache');
  }

  goOfflineData() {
    this.navCtrl.push('OfflineData');
  }

  goOfflineWork(pageType) {
    this.navCtrl.push('OfflineWork', {
      "pageType": pageType
    });
  }

}
